; program to display integer as 8 hex digits
; Author:  R. Detmer
; Date:    revised 11/97

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

include io.h            ; header file for input/output

cr      equ     0dh     ; carriage return character
Lf      equ     0ah     ; line feed

.STACK  4096             ; reserve 4096-byte stack

.DATA                   ; reserve storage for data
prompt      BYTE   "Enter a number:  ",0
number      BYTE   20 DUP (?)
result      BYTE   cr,Lf,"The 2's complement representation is "
hexOut      BYTE   8 DUP (?),cr,Lf,0

.CODE                               ; start of main program code
_start:
            output prompt           ; prompt for number
            input  number,20        ; read ASCII characters
            atod   number           ; convert to integer

            lea    ebx,hexOut+7     ; address for last character
            mov    ecx,8            ; number of characters
forCount:   mov    edx,eax          ; copy pattern
            and    edx,0000000fh    ; zero all but last hex digit
            cmp    edx,9            ; digit?
            jnle   elseLetter       ; letter if not
            or     edx,30h          ; convert to character
            jmp    endifDigit
elseLetter: add    edx,'A'-10       ; convert to letter
endifDigit:
            mov    BYTE PTR [ebx],dl ; copy character to memory
            dec    ebx              ; point at next character
            shr    eax,4            ; shift one hex digit right
            loop   forCount         ; repeat

            output result           ; output label and hex value

            INVOKE  ExitProcess, 0  ; exit with return code 0
PUBLIC _start                       ; make entry point public
END                                 ; end of source code

